package com.saohuobang.test.model.core;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.io.Serializable;

public class APIResponse<T> implements Serializable {

    private static final long serialVersionUID = 5241526151768786394L;

    @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
    private String msg = "";
    @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
    private int resCode = 1;
    @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
    private T data;

    public APIResponse() {
    }

    private APIResponse(T t) {
        this.data = t;
        this.resCode = 1;
    }

    private APIResponse(String msg, T t) {
        this.msg = msg;
        this.data = t;
        this.resCode = -1;
    }

    private APIResponse(int resCode, String msg, T t) {
        this.msg = msg;
        this.resCode = resCode;
        this.data = t;
    }

    public static <T> APIResponse<T> returnSuccess() {
        return new APIResponse<T>(null);
    }

    public static <T> APIResponse<T> returnSuccess(T t) {
        return new APIResponse<T>(t);
    }

    public static <T> APIResponse<T> returnFail(String errmsg) {
        return new APIResponse<T>(errmsg, null);
    }

    public static <T> APIResponse<T> returnFail(String errmsg, T t) {
        return new APIResponse<T>(errmsg, t);
    }

    public static <T> APIResponse<T> returnFail(int errcode , String errmsg) {
        return new APIResponse<T>(errcode, errmsg,null);
    }

    public static <T> APIResponse<T> returnFail(int errcode , String errmsg , T t) {
        return new APIResponse<T>(errcode, errmsg,t);
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getResCode() {
        return resCode;
    }

    public void setResCode(int resCode) {
        this.resCode = resCode;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((this.data == null) ? 0 : this.data.hashCode());
        result = prime * result + ((msg == null) ? 0 : msg.hashCode());
        result = prime * result + resCode;
        return result;
    }

    @Override
    public String toString() {
        return "APIResponse [" +
                ", errmsg='" + msg + '\'' +
                ", errcode=" + resCode +
                ", data=" + data +
                ']';
    }
}
